package com.laoguo.accept;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class RabbitmqReceiver {
    @RabbitListener(queues = "guojunjie")
    public void process(String init, Message message, Channel channel) {
        try {
            log.info("消息收到:  " + init);
        }catch (Exception e){
            log.error(e.getMessage());
        }
        finally {
            try {
                log.info("确认消息:   " + message.getMessageProperties().getDeliveryTag());
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            }catch (IOException e){
                log.error(e.getMessage() + ": " + e);
            }

        }
    }

    @RabbitListener(queues = "delay_queue")
    public void process1(String msg,Message message, Channel channel){
        if (msg != null){
            try{
                log.info("消息收到： " + msg);
            }catch (Exception e){
                log.error(e.getMessage());
            }finally {
                try {
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
                } catch (IOException e) {
                    log.error(e.getMessage() + ": " + e);
                }
            }
        }
    }
}
